{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.总结关于字典、集合的所有方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1字典"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1.1创建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第一种方式\n",
    "dict = {'a':1,'b':2,'c':3}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a': 1, 'b': 2, 'c': 3}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 第二种方式\n",
    "dict1 = {'abc':1}\n",
    "dict2 = {'def':2}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'abc': 1}"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'def': 2}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1.2问字典里的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict['name']: 王源\n",
      "dict['age']: 19\n",
      "dict['class']: first\n"
     ]
    }
   ],
   "source": [
    "dict = {'name':'王源','age':19,'class':'first'}\n",
    "print(\"dict['name']:\",dict['name'])\n",
    "print(\"dict['age']:\",dict['age'])\n",
    "print(\"dict['class']:\",dict['class'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1.3修改字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict['age']:  20\n",
      "dict['school']:  伯克利音乐学院\n"
     ]
    }
   ],
   "source": [
    "dict = {'name':'王源','age':19,'class':'first'}\n",
    "dict['age'] = 20\n",
    "dict['school'] = \"伯克利音乐学院\"\n",
    "print (\"dict['age']: \", dict['age'])\n",
    "print (\"dict['school']: \", dict['school'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1.4删除字典元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '王源', 'age': 19}"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除键：class\n",
    "dict = {'name':'王源','age':19,'class':'first'}\n",
    "del dict['class']\n",
    "dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "cannot delete function call (<ipython-input-30-e5a8b9d37c9a>, line 2)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-30-e5a8b9d37c9a>\"\u001b[1;36m, line \u001b[1;32m2\u001b[0m\n\u001b[1;33m    del clear()\u001b[0m\n\u001b[1;37m        ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m cannot delete function call\n"
     ]
    }
   ],
   "source": [
    "# 清空字典\n",
    "dict = {'name':'王源','age':19,'class':'first'}\n",
    "del clear()\n",
    "dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 删除字典\n",
    "dict = {'name':'王源','age':19,'class':'first'}\n",
    "del dict\n",
    "dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.1.5字典的特性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': '林新伟', 'age': 19}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 不允许同一个键出现两次。创建时如果同一个键被赋值两次，后一个值会被记住\n",
    "dict = {'name':'王源','age':19,'name':'林新伟'}\n",
    "dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "unhashable type: 'list'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-34-74129fe38e2a>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m# 键必须不可变，所以可以用数字，字符串或元组充当，而用列表就不行\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mdict\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m{\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'name'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;34m'王源'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m'age'\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;36m19\u001b[0m\u001b[1;33m}\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      3\u001b[0m \u001b[0mdict\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: unhashable type: 'list'"
     ]
    }
   ],
   "source": [
    "# 键必须不可变，所以可以用数字，字符串或元组充当，而用列表就不行\n",
    "dict = {['name']: '王源', 'age': 19}\n",
    "dict"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2集合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.1创建集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apple', 'orange', 'pear'}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第一种方法：\n",
    "a = {'apple', 'orange', 'pear',}\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'orange', 'apple', 'pear'}\n"
     ]
    }
   ],
   "source": [
    "# 第二种方法：\n",
    "a = set((\"apple\", \"orange\", \"pear\"))\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.2添加元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'orange', 'apple', 'pear', 'banana'}\n"
     ]
    }
   ],
   "source": [
    "# 第一种方法：\n",
    "a = set((\"apple\", \"orange\", \"pear\"))\n",
    "a.add(\"banana\")\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'orange', 1, 'apple', 3, 'pear'}\n"
     ]
    }
   ],
   "source": [
    "# 第二种方法：\n",
    "a = set((\"apple\", \"orange\", \"pear\"))\n",
    "a.update({1,3})\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.3移除元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'orange', 'pear'}\n"
     ]
    }
   ],
   "source": [
    "# 第一种方法：将元素 x 从集合 s 中移除，如果元素不存在，则会发生错误。\n",
    "a = set((\"apple\", \"orange\", \"pear\"))\n",
    "a.remove(\"apple\")\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "'banana'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-46-3c7a276912e1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0ma\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mset\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"apple\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"orange\"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;34m\"pear\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mremove\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"banana\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mKeyError\u001b[0m: 'banana'"
     ]
    }
   ],
   "source": [
    "a = set((\"apple\", \"orange\", \"pear\"))\n",
    "a.remove(\"banana\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'orange', 'apple', 'pear'}\n"
     ]
    }
   ],
   "source": [
    "# 第二种方法：将元素 x 从集合 s 中移除，如果元素不存在，不会发生错误。\n",
    "a = set((\"apple\", \"orange\", \"pear\"))\n",
    "a.discard(\"banana\")\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "orange\n",
      "{'apple', 'pear'}\n"
     ]
    }
   ],
   "source": [
    "# 第三种方法：随机删除集合中的一个元素\n",
    "a = set((\"apple\", \"orange\", \"pear\"))\n",
    "x = a.pop()\n",
    "print(x)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.4计算集合元素个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = set((\"apple\", \"orange\", \"pear\"))\n",
    "len(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.5清空集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "set()\n"
     ]
    }
   ],
   "source": [
    "a = set((\"apple\", \"orange\", \"pear\"))\n",
    "a.clear()\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.2.6判断元素是否在集合中存在"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = set((\"apple\", \"orange\", \"pear\"))\n",
    "\"apple\" in a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = set((\"apple\", \"orange\", \"pear\"))\n",
    "\"banana\" in a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.猜数字练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入数字：········\n",
      "<class 'int'>\n",
      "猜一猜：13\n",
      "<class 'int'>\n",
      "恭喜你答对了!\n"
     ]
    }
   ],
   "source": [
    "import getpass\n",
    "card = int(getpass.getpass(\"请输入数字：\"))\n",
    "print(type(card))\n",
    "count = 0\n",
    "\n",
    "while True:\n",
    "    if count <3:\n",
    "        user_guess = int(input(\"猜一猜：\"))\n",
    "        print(type(user_guess))\n",
    "        if card == user_guess:\n",
    "            print(\"恭喜你答对了!\")\n",
    "            break\n",
    "        elif card > user_guess:\n",
    "            print(\"太小了！\")\n",
    "        else:\n",
    "            print(\"太大了！\")\n",
    "    else:\n",
    "        print(\"猜了这么多次都猜不出来\")\n",
    "        break\n",
    "    count = count+1\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.课本代码练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The data file is missing!\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "if os.path.exists('sketch.txt'):\n",
    "    data = open('sketch.txt')\n",
    "    for each_line in data:\n",
    "        if not each_line.find(':') == -1:\n",
    "            (role,line_spoken) = each_line.split(':',1)\n",
    "            print(role,end='')\n",
    "            print(' said:',end='')\n",
    "            print(line_spoken,end='')\n",
    "    data.close()\n",
    "else:\n",
    "    print('The data file is missing!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 150,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The data fail is missing!\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    data = open('sketch.txt')\n",
    "    for each_line in data:\n",
    "        try:\n",
    "            (role,line_spoken) = each_line.split(':',1)\n",
    "            print(role,end='')\n",
    "            print('said:',end='')\n",
    "            print(line_spoken,end='')\n",
    "        except:\n",
    "            pass\n",
    "    data.close()\n",
    "except:\n",
    "    print('The data fail is missing!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The data fail is missing!\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    data = open('sketch.txt')\n",
    "    for each_line in data:\n",
    "        try:\n",
    "            (role,line_spoken) = each_line.split(':',1)\n",
    "            print(role,end='')\n",
    "            print('said:',end='')\n",
    "            print(line_spoken,end='')\n",
    "        except ValueError:\n",
    "            pass\n",
    "    data.close()\n",
    "except IOError:\n",
    "    print('The data fail is missing!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The datafile is missing!\n",
      "[]\n",
      "[]\n"
     ]
    }
   ],
   "source": [
    "man = []\n",
    "other = []\n",
    "try:\n",
    "    data = open('sketch.txt')\n",
    "    for each_line in data:\n",
    "        try:\n",
    "            (role,line_spoken) = each_line.split(':',1)\n",
    "            line_spoken = line_spoken.strip()\n",
    "            if role == 'Man':\n",
    "                man.append(line_spoken)\n",
    "            elif role == 'Other Man':\n",
    "                other.append(line_spoken)\n",
    "        except ValueError:\n",
    "            pass\n",
    "    data.close()\n",
    "except IOError:\n",
    "    print('The datafile is missing!')\n",
    "print(man)\n",
    "print(other)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.字典查询练习:   \n",
    "> * Azure API 提供一张含有>=3张脸（3个以上faceid）的图片，先用json()获取内容（results），在用数据结构查询的方式查找到：眼镜、肤色、微笑指数、头发颜色、年龄、性别这几项数据，并分别存进变量/自定义数据结构（如字典等）当中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "# set to your own subscription key value\n",
    "subscription_key = \"d6d151266e1448de9aa63beaa30edd99\"\n",
    "assert subscription_key\n",
    "\n",
    "# replace <My Endpoint String> with the string from your endpoint URL\n",
    "face_api_url = 'https://api-wy.cognitiveservices.azure.com//face/v1.0/detect'\n",
    "\n",
    "# 请求正文body\n",
    "image_url = 'http://img.idol001.com/origin/2017/10/19/65aceadb9d4742835263572db2599b611508416428.jpg'\n",
    "\n",
    "headers = {'Ocp-Apim-Subscription-Key': subscription_key}\n",
    "\n",
    "# 请求参数parameters\n",
    "params = {\n",
    "    'returnFaceId': 'true',\n",
    "    'returnFaceLandmarks': 'false',\n",
    "    # 可选参数,请仔细阅读API文档\n",
    "    'returnFaceAttributes': 'age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise',\n",
    "}\n",
    "\n",
    "response = requests.post(face_api_url, params=params,\n",
    "                         headers=headers, json={\"url\": image_url})\n",
    "# json.dumps 将json--->bytes\n",
    "response"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceId': 'd45bfa08-9d09-4674-a0e1-c67ec7189a19',\n",
       " 'faceRectangle': {'top': 167, 'left': 249, 'width': 88, 'height': 88},\n",
       " 'faceAttributes': {'smile': 1.0,\n",
       "  'headPose': {'pitch': -5.9, 'roll': 18.0, 'yaw': 21.8},\n",
       "  'gender': 'male',\n",
       "  'age': 30.0,\n",
       "  'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "  'glasses': 'NoGlasses',\n",
       "  'emotion': {'anger': 0.0,\n",
       "   'contempt': 0.0,\n",
       "   'disgust': 0.0,\n",
       "   'fear': 0.0,\n",
       "   'happiness': 1.0,\n",
       "   'neutral': 0.0,\n",
       "   'sadness': 0.0,\n",
       "   'surprise': 0.0},\n",
       "  'blur': {'blurLevel': 'medium', 'value': 0.33},\n",
       "  'exposure': {'exposureLevel': 'goodExposure', 'value': 0.61},\n",
       "  'noise': {'noiseLevel': 'low', 'value': 0.0},\n",
       "  'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "  'accessories': [],\n",
       "  'occlusion': {'foreheadOccluded': False,\n",
       "   'eyeOccluded': False,\n",
       "   'mouthOccluded': False},\n",
       "  'hair': {'bald': 0.04,\n",
       "   'invisible': False,\n",
       "   'hairColor': [{'color': 'brown', 'confidence': 0.99},\n",
       "    {'color': 'blond', 'confidence': 0.96},\n",
       "    {'color': 'red', 'confidence': 0.29},\n",
       "    {'color': 'gray', 'confidence': 0.29},\n",
       "    {'color': 'black', 'confidence': 0.05},\n",
       "    {'color': 'other', 'confidence': 0.03},\n",
       "    {'color': 'white', 'confidence': 0.0}]}}}"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第一位\n",
    "results[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'NoGlasses'"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 眼镜\n",
    "results[0][\"faceAttributes\"][\"glasses\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 微笑指数\n",
    "results[0][\"faceAttributes\"][\"emotion\"][\"happiness\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'color': 'brown', 'confidence': 0.99},\n",
       " {'color': 'blond', 'confidence': 0.96},\n",
       " {'color': 'red', 'confidence': 0.29},\n",
       " {'color': 'gray', 'confidence': 0.29},\n",
       " {'color': 'black', 'confidence': 0.05},\n",
       " {'color': 'other', 'confidence': 0.03},\n",
       " {'color': 'white', 'confidence': 0.0}]"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 头发颜色\n",
    "results[0][\"faceAttributes\"][\"hair\"][\"hairColor\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "30.0"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 年龄\n",
    "results[0][\"faceAttributes\"][\"age\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'male'"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 性别\n",
    "results[0][\"faceAttributes\"][\"gender\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'眼镜': 'NoGlasses',\n",
       " '微笑指数': 1.0,\n",
       " '头发颜色': [{'color': 'brown', 'confidence': 0.99},\n",
       "  {'color': 'blond', 'confidence': 0.96},\n",
       "  {'color': 'red', 'confidence': 0.29},\n",
       "  {'color': 'gray', 'confidence': 0.29},\n",
       "  {'color': 'black', 'confidence': 0.05},\n",
       "  {'color': 'other', 'confidence': 0.03},\n",
       "  {'color': 'white', 'confidence': 0.0}],\n",
       " '年龄': 30.0,\n",
       " '性别': 'male'}"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict1 = {'眼镜':results[0][\"faceAttributes\"][\"glasses\"],'微笑指数':results[0][\"faceAttributes\"][\"emotion\"][\"happiness\"],'头发颜色':results[0][\"faceAttributes\"][\"hair\"][\"hairColor\"],'年龄':results[0][\"faceAttributes\"][\"age\"],'性别':results[0][\"faceAttributes\"][\"gender\"]}\n",
    "dict1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceId': '859e4fe4-e464-47ae-b452-cf07cfc2ce11',\n",
       " 'faceRectangle': {'top': 223, 'left': 394, 'width': 83, 'height': 83},\n",
       " 'faceAttributes': {'smile': 0.006,\n",
       "  'headPose': {'pitch': -6.5, 'roll': 1.1, 'yaw': 1.7},\n",
       "  'gender': 'male',\n",
       "  'age': 21.0,\n",
       "  'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "  'glasses': 'NoGlasses',\n",
       "  'emotion': {'anger': 0.0,\n",
       "   'contempt': 0.0,\n",
       "   'disgust': 0.0,\n",
       "   'fear': 0.0,\n",
       "   'happiness': 0.006,\n",
       "   'neutral': 0.991,\n",
       "   'sadness': 0.003,\n",
       "   'surprise': 0.0},\n",
       "  'blur': {'blurLevel': 'low', 'value': 0.06},\n",
       "  'exposure': {'exposureLevel': 'goodExposure', 'value': 0.55},\n",
       "  'noise': {'noiseLevel': 'low', 'value': 0.0},\n",
       "  'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "  'accessories': [],\n",
       "  'occlusion': {'foreheadOccluded': False,\n",
       "   'eyeOccluded': False,\n",
       "   'mouthOccluded': False},\n",
       "  'hair': {'bald': 0.06,\n",
       "   'invisible': False,\n",
       "   'hairColor': [{'color': 'brown', 'confidence': 0.99},\n",
       "    {'color': 'black', 'confidence': 0.93},\n",
       "    {'color': 'red', 'confidence': 0.21},\n",
       "    {'color': 'blond', 'confidence': 0.18},\n",
       "    {'color': 'other', 'confidence': 0.14},\n",
       "    {'color': 'gray', 'confidence': 0.1},\n",
       "    {'color': 'white', 'confidence': 0.0}]}}}"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二位\n",
    "results[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'NoGlasses'"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 眼镜\n",
    "results[1][\"faceAttributes\"][\"glasses\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.006"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 微笑指数\n",
    "results[1][\"faceAttributes\"][\"emotion\"][\"happiness\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'color': 'brown', 'confidence': 0.99},\n",
       " {'color': 'black', 'confidence': 0.93},\n",
       " {'color': 'red', 'confidence': 0.21},\n",
       " {'color': 'blond', 'confidence': 0.18},\n",
       " {'color': 'other', 'confidence': 0.14},\n",
       " {'color': 'gray', 'confidence': 0.1},\n",
       " {'color': 'white', 'confidence': 0.0}]"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 头发颜色\n",
    "results[1][\"faceAttributes\"][\"hair\"][\"hairColor\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21.0"
      ]
     },
     "execution_count": 125,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 年龄\n",
    "results[1][\"faceAttributes\"][\"age\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'male'"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 性别\n",
    "results[1][\"faceAttributes\"][\"gender\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'眼镜': 'NoGlasses',\n",
       " '微笑指数': 0.006,\n",
       " '头发颜色': [{'color': 'brown', 'confidence': 0.99},\n",
       "  {'color': 'black', 'confidence': 0.93},\n",
       "  {'color': 'red', 'confidence': 0.21},\n",
       "  {'color': 'blond', 'confidence': 0.18},\n",
       "  {'color': 'other', 'confidence': 0.14},\n",
       "  {'color': 'gray', 'confidence': 0.1},\n",
       "  {'color': 'white', 'confidence': 0.0}],\n",
       " '年龄': 21.0,\n",
       " '性别': 'male'}"
      ]
     },
     "execution_count": 139,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict2 = {'眼镜':results[1][\"faceAttributes\"][\"glasses\"],'微笑指数':results[1][\"faceAttributes\"][\"emotion\"][\"happiness\"],'头发颜色':results[1][\"faceAttributes\"][\"hair\"][\"hairColor\"],'年龄':results[1][\"faceAttributes\"][\"age\"],'性别':results[1][\"faceAttributes\"][\"gender\"]}\n",
    "dict2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceId': '75131ce2-b2db-4a87-adcf-72bc26a8d87f',\n",
       " 'faceRectangle': {'top': 245, 'left': 587, 'width': 78, 'height': 78},\n",
       " 'faceAttributes': {'smile': 1.0,\n",
       "  'headPose': {'pitch': -6.5, 'roll': 4.3, 'yaw': 18.7},\n",
       "  'gender': 'male',\n",
       "  'age': 28.0,\n",
       "  'facialHair': {'moustache': 0.1, 'beard': 0.1, 'sideburns': 0.1},\n",
       "  'glasses': 'NoGlasses',\n",
       "  'emotion': {'anger': 0.0,\n",
       "   'contempt': 0.0,\n",
       "   'disgust': 0.0,\n",
       "   'fear': 0.0,\n",
       "   'happiness': 1.0,\n",
       "   'neutral': 0.0,\n",
       "   'sadness': 0.0,\n",
       "   'surprise': 0.0},\n",
       "  'blur': {'blurLevel': 'medium', 'value': 0.34},\n",
       "  'exposure': {'exposureLevel': 'goodExposure', 'value': 0.54},\n",
       "  'noise': {'noiseLevel': 'low', 'value': 0.0},\n",
       "  'makeup': {'eyeMakeup': False, 'lipMakeup': False},\n",
       "  'accessories': [],\n",
       "  'occlusion': {'foreheadOccluded': False,\n",
       "   'eyeOccluded': False,\n",
       "   'mouthOccluded': False},\n",
       "  'hair': {'bald': 0.26,\n",
       "   'invisible': False,\n",
       "   'hairColor': [{'color': 'black', 'confidence': 0.94},\n",
       "    {'color': 'gray', 'confidence': 0.91},\n",
       "    {'color': 'blond', 'confidence': 0.6},\n",
       "    {'color': 'other', 'confidence': 0.43},\n",
       "    {'color': 'brown', 'confidence': 0.31},\n",
       "    {'color': 'red', 'confidence': 0.01},\n",
       "    {'color': 'white', 'confidence': 0.0}]}}}"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第三位\n",
    "results[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'NoGlasses'"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 眼镜\n",
    "results[2][\"faceAttributes\"][\"glasses\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 微笑指数\n",
    "results[2][\"faceAttributes\"][\"emotion\"][\"happiness\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'color': 'black', 'confidence': 0.94},\n",
       " {'color': 'gray', 'confidence': 0.91},\n",
       " {'color': 'blond', 'confidence': 0.6},\n",
       " {'color': 'other', 'confidence': 0.43},\n",
       " {'color': 'brown', 'confidence': 0.31},\n",
       " {'color': 'red', 'confidence': 0.01},\n",
       " {'color': 'white', 'confidence': 0.0}]"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 头发颜色\n",
    "results[2][\"faceAttributes\"][\"hair\"][\"hairColor\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "28.0"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 年龄\n",
    "results[2][\"faceAttributes\"][\"age\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'male'"
      ]
     },
     "execution_count": 132,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 性别\n",
    "results[2][\"faceAttributes\"][\"gender\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'眼镜': 'NoGlasses',\n",
       " '微笑指数': 1.0,\n",
       " '头发颜色': [{'color': 'black', 'confidence': 0.94},\n",
       "  {'color': 'gray', 'confidence': 0.91},\n",
       "  {'color': 'blond', 'confidence': 0.6},\n",
       "  {'color': 'other', 'confidence': 0.43},\n",
       "  {'color': 'brown', 'confidence': 0.31},\n",
       "  {'color': 'red', 'confidence': 0.01},\n",
       "  {'color': 'white', 'confidence': 0.0}],\n",
       " '年龄': 28.0,\n",
       " '性别': 'male'}"
      ]
     },
     "execution_count": 140,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dict3 = {'眼镜':results[2][\"faceAttributes\"][\"glasses\"],'微笑指数':results[2][\"faceAttributes\"][\"emotion\"][\"happiness\"],'头发颜色':results[2][\"faceAttributes\"][\"hair\"][\"hairColor\"],'年龄':results[2][\"faceAttributes\"][\"age\"],'性别':results[2][\"faceAttributes\"][\"gender\"]}\n",
    "dict3"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
